package com.hsd.app.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsd.app.model.HsdDssRelation;
import com.hsd.app.model.HsdDssinfo;
import com.hsd.app.service.IDataManageService;
import com.hsd.commons.model.HsdAccount;
import com.hsd.core.page.entity.PageParams;
import com.hsd.core.page.entity.Pagination;
import com.hsd.core.utils.TimeUtil;


@Controller
@RequestMapping("/datamanage")
public class DssManageAction extends PLBaseAction{
	@Autowired
	private IDataManageService hpdataService;
	
	
	/**
	 * 数据管理 ：加载病症证信息列表
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/loadDssList.do")
	public String loadDssList(PageParams pageParams,HttpServletRequest request,ModelMap model){
		String dssname = request.getParameter("dssname"); 
		String catagory = request.getParameter("catagory"); 
		Map paramMap = new HashMap();
		paramMap.put("dssname", dssname);
		paramMap.put("catagory", catagory);
		Pagination page = hpdataService.loadDssList(pageParams,paramMap);
		
		model.addAttribute("page",page);
		model.addAttribute("dssname",dssname); 
		return "app/data/data-dss-list";
	} 
	/**
	 * 新增一个病症证
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/addOneDss.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String addOneDss(HsdDssinfo dssobj,HttpServletRequest request,ModelMap model){
		Map query = new HashMap();
		query.put("dssname", dssobj.getDssname());
		query.put("youxiaobj", new Long(1));
		Object existData = hpdataService.getUniqueObject(HsdDssinfo.class, query);
		if(null != existData){
			return "该名称已存在！";
		} 
		dssobj.setId(this.hpdataService.getSeqNextVal("seq_hsd_dssinfo_id"));
		HsdAccount accInfo = this.getCurrentuser(request); 
		dssobj.setYouxiaobj(PLCONSTANTS.YOUXIAOBJ_YX);
		dssobj.setOpertime(TimeUtil.getCurrentDate());
		if(null != accInfo){
			dssobj.setUserid(accInfo.getId());
			dssobj.setUsername(accInfo.getUsername());
		}
		hpdataService.save(dssobj);  
		return "保存成功！";
	}
	/**
	 * 查询一个病症证
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/selectOneDss.do")
	public  String  selectOneDss(HttpServletRequest request,ModelMap model){ 
		String id = request.getParameter("id");
		HsdDssinfo dssinfo = (HsdDssinfo)hpdataService.findByID(HsdDssinfo.class, new Long(id));
		model.put("dssinfo", dssinfo);
		return "app/data/insert-one-dss";
	}
	/**
	 * 修改一个病症证
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/editOneDss.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String editOneDss(HsdDssinfo dssobj,HttpServletRequest request,ModelMap model){ 
		String id = request.getParameter("id");
		HsdDssinfo updateHsdDssinfo = (HsdDssinfo) this.hpdataService.findByID(HsdDssinfo.class, new Long(id)); 
		updateHsdDssinfo.setDssname(dssobj.getDssname());
		updateHsdDssinfo.setSynname(dssobj.getSynname());
		updateHsdDssinfo.setDssname_eng(dssobj.getDssname_eng());
		updateHsdDssinfo.setCatagory(dssobj.getCatagory());
		updateHsdDssinfo.setComfrom(dssobj.getComfrom());
		updateHsdDssinfo.setCodestr(dssobj.getCodestr());
		updateHsdDssinfo.setDesccontent(dssobj.getDesccontent());
		hpdataService.save(updateHsdDssinfo);
		return "修改成功！";
	}
	/**
	 * 删除病症证
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/delOneDss.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String delOneOrg(HttpServletRequest request,ModelMap model){
		String id = request.getParameter("id"); 
		Map paramMap = new HashMap();
		paramMap.put("dssid", new Long(id));
		List datalist  = this.hpdataService.findList(HsdDssRelation.class, paramMap);
		if(datalist != null && datalist.size() >0){
			return "该数据" +
					"已经使用，不能删除！";
		}
		HsdDssinfo dssObj = (HsdDssinfo)hpdataService.findByID(HsdDssinfo.class, new Long(id));
		dssObj.setYouxiaobj(PLCONSTANTS.YOUXIAOBJ_WX);
		hpdataService.saveObject(dssObj);
		return "删除成功！";
	}
}
